/*
* 正权有向图
* 求S->F的最短路径 + 最短路径+1 的路径数量
* 参考DP中的最大值 次大值求法
*/
#pragma GCC optimize("O1,O2,O3,Ofast")
#pragma GCC optimize("no-stack-protector,unroll-loops,fast-math,inline")
#pragma GCC target("avx,avx2,fma")
#pragma GCC target("sse,sse2,sse3,sse4,sse4.1,sse4.2,ssse3")

#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
// #define ONLINE_GUDGE
using namespace std;
using PII = pair<int, int>;
#define type first
#define id second
const int N = 1e6+10, M = N << 1, INF = 0x3f3f3f3f;

int n, m, S, F;
int h[N], e[M], w[M], ne[M], idx;
int dist[2][N];
bool st[2][N]; 
int cnt[2][N];

struct cmp {
    bool operator ()(PII x,PII y) {
        return dist[x.type][x.id] > dist[y.type][y.id];
    }
};

void AddEdge(int a, int b, int c)
{ e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++; }

void dijkstra()
{   
    dist[0][S] = 0,cnt[0][S] = 1;

    priority_queue <PII,vector <PII>,cmp> heap;
    heap.push ({0,S});

    while (heap.size ()) {
        auto [type, u] = heap.top (); heap.pop();
        if(st[type][u]) continue;
        st[type][u] = true;

        for(int i = h[u]; ~i; i = ne[i])
        {
            int v = e[i], distance = dist[type][u] + w[i],c = cnt[type][u];
            // st[v] = false; // 当前点还有可能被优化
            if(distance < dist[0][v]){ // 最优解被优化
                // 最优解 -> 次优解 
                dist[1][v] = dist[0][v]; 
                cnt[1][v] = cnt[0][v];
                heap.push ({1,v}); // 后续的次优解被更新

                // 次优解更新                
                dist[0][v] = distance;
                cnt[0][v] = c;
                heap.push ({0,v});
            }
            else if (distance == dist[0][v]) cnt[0][v] += c; // 本身就是最优解
            else if (distance < dist[1][v]) { // 次优解更新 
                dist[1][v] = distance;
                cnt[1][v] = c;
                heap.push ({1,v});
            }
            else if (distance == dist[1][v]) cnt[1][v] += c; // 本身就是次优解
        }
    }
}


int main()
{
    #ifdef ONLINE_JUDGE

    #else
        freopen("./in.txt", "r", stdin);
    #endif

    ios::sync_with_stdio(0); cin.tie(0);

    int T; cin >> T;

    while(T--)
    {
        memset(h, -1, sizeof h);
        memset(cnt, 0, sizeof cnt);
        memset(st, 0, sizeof st);
        memset(dist, 0x3f, sizeof dist);
        idx = 0;


        cin >> n >> m;  

        while(m--){
            int a, b, c; cin >> a >> b >> c;

            AddEdge(a, b, c); // 单向图
        }
        cin >> S >> F; // st -> en

        dijkstra();
        int ans = cnt[0][F];
        if (dist[1][F] == dist[0][F] + 1) ans += cnt[1][F];
        cout << ans << endl;
    }
    

    return 0;
}